package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/9 14:26
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;
    //查询收获地址列表
    /*
        请求方式:GET
        请求地址：http://localhost:8081/address/list
        返回值： List<Address>
     */
    @GetMapping("/address/list")
    public ResultInfo list(){
        List<Address> list =addressService.list();
        return ResultInfo.success(list);
    }
    //新增收获地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address){
        addressService.add(address);
        return ResultInfo.success();
    }
    //设置默认地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Address address){
        addressService.setDefault(address);
        return ResultInfo.success();
    }
    //查询默认地址
    @GetMapping("/address/default")
    public ResultInfo findDefault(){
        Address address=addressService.findDefault();
        return ResultInfo.success(address);
    }
    //修改收货地址-回显
    @GetMapping("/address/{addressId}")
    public ResultInfo findByAddressId(@PathVariable("addressId") Long addressId){
        Address address=addressService.findByAddressId(addressId);
        return ResultInfo.success(address);
    }
    //修改收货地址-内容
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address){
        addressService.updateAddress(address);
        return ResultInfo.success();
    }
}

